package com.ijiuchuang.jc_localization.presenter.tools;

import android.content.Context;

import com.ijiuchuang.jc_localization.bean.entity.BleDevice;
import com.ijiuchuang.jc_localization.ble.BleCallback;
import com.ijiuchuang.jc_localization.ble.BleUtil;
import com.ijiuchuang.jc_localization.contract.tools.ToolsGuardsContract;
import com.ijiuchuang.jc_localization.model.tools.ToolsGuardsModel;
import com.ijiuchuang.jc_localization.tools.utils.Log;

/**
 * Created by Veev on 2016/7/6
 *
 * @link 384506557@qq.com
 */
public class ToolsGuardsPresenter implements ToolsGuardsContract.Presenter {

    private Context mContext;
    private ToolsGuardsContract.View mView;
    private ToolsGuardsContract.Model mModel;

    public ToolsGuardsPresenter(Context mContext, ToolsGuardsContract.View mView) {
        this.mContext = mContext;
        this.mView = mView;
        this.mModel = new ToolsGuardsModel();

        mView.setPresenter(this);
    }

    @Override
    public void start() {
        // start here
        BleUtil.bleInit(mContext, new BleCallback.BleInitListener() {
            @Override
            public void onSuccess() {
                // 功能正常
                mView.setFabIcon(mModel.getBleEnableRes());
            }

            @Override
            public void onDisEnabled() {
                // 提示开启蓝牙
                mView.openBle();
            }

            @Override
            public void onFailure() {
                // 蓝牙功能异常，提示，并退出
            }
        });

        startScan(mView.getDevice());
        mView.setDeviceName(mView.getDevice().getName());
        mView.setMac(mView.getDevice().getMac());
    }

    @Override
    public void startScan(BleDevice bleDevice) {
        //
        mModel.startScan(mContext, bleDevice, new ToolsGuardsContract.Model.GuardsListener() {
            @Override
            public void isSafety(Boolean isSafety) {
                if (isSafety) {
                    mView.setState("安全");
                } else {
                    mView.setState("丢失");
                }
            }

            @Override
            public void lostCount(int count) {
                mView.setCount("丢失：" + count + "次");
            }

            @Override
            public void rssiUpdate(int rssi) {
                mView.setRssiOrigin("Rssi: " + rssi);
            }

            @Override
            public void heartRateUpdate(int heartRate) {
                mView.setHeartRate("" + heartRate);
            }

            @Override
            public void powerUpdate(int power) {
                mView.setPower("" + power);
            }
        });
    }

    @Override
    public void stopScan() {
        mModel.stopScan(mContext);
    }
}